package copyengine.utils
{
	import copyengine.resource.CEResConfig;
	import copyengine.resource.CEResManger;
	import copyengine.resource.dto.ResDTOFactory;
	import copyengine.resource.dto.ResObjectRequestParamDTO;
	import copyengine.resource.file.CEPicResourceFile;
	import copyengine.resource.file.CESwfResourceFile;
	import copyengine.resource.file.ICEResFile;
	import copyengine.resource.file.sound.CESound;
	import copyengine.utils.cache.CECacheDisplayObjectManger;
	import copyengine.utils.debug.CELog;

	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.ByteArray;

	public class ResUtils
	{
		public function ResUtils()
		{
		}

		public static function getCacheDisplayObject(_symbolName:String, _fileName:String, _cacheType:int = CEResConfig.CACHE_TYPE_AUTO):*
		{
			var resFile:ICEResFile = getResFileByFileName(_fileName);
			if (resFile != null)
			{
				if (resFile.loadState == CEResConfig.LOAD_STATE_LOADED)
				{
					return CECacheDisplayObjectManger.instance.createCacheInstance(_symbolName, _fileName, _cacheType);
				}
				else
				{
					return getLazyLoadCacheDisplayObject(_symbolName, _fileName, _cacheType);
				}
			}
			else
			{
				CELog.log("Can't find file name : " + _fileName);
				return null;
			}
		}

		public static function getSound(_fileName:String):CESound
		{
			var cesound:CESound = new CESound();
			cesound.initialize(_fileName);
			return cesound;
		}

		public static function getByteArray(_fileName:String):ByteArray
		{
			var resFile:ICEResFile = getResFileByFileName(_fileName);
			if (resFile != null && resFile.loadState == CEResConfig.LOAD_STATE_LOADED)
			{
				return resFile.getObject(null) as ByteArray
			}
			CELog.log("ResUtils->getByteArrayData Can't find file name : " + _fileName + " or file not loaded");
			return null;
		}

		private static function getLazyLoadCacheDisplayObject(_symbolName:String, _fileName:String, _cacheType:int):DisplayObject
		{
			var dto:ResObjectRequestParamDTO = ResDTOFactory.getCacheDisplayObjectDTO(_symbolName, _cacheType);
			return getDisplayObject(_fileName, dto);
		}

		/**
		 * <b>寻找swf中是否含有某个元件</b>
		 * </br><b>注意!!! </b>
		 * <li>只有swf类型的文件才可以调用该函数,否者一概返回false
		 * <li>该swf需要在ResourceScan时候标记为 symbolType="Symbol" 这样在xml文件中才会生成所有导出类的名称
		 */
		public static function isSymbolExist(_symbolName:String, _fileName:String):Boolean
		{
			var resFile:ICEResFile = getResFileByFileName(_fileName);
			if (resFile && resFile is CESwfResourceFile)
			{
				return (resFile as CESwfResourceFile).isSymbolExist(_symbolName);
			}
			return false;
		}

		public static function getMovieClip(_symbolName:String, _fileName:String):MovieClip
		{
			var dto:ResObjectRequestParamDTO = ResDTOFactory.getMovieClipDTO(_symbolName);
			return getDisplayObject(_fileName, dto) as MovieClip;
		}

		public static function getSprite(_symbolName:String, _fileName:String):Sprite
		{
			var dto:ResObjectRequestParamDTO = ResDTOFactory.getSpriteDTO(_symbolName);
			return getDisplayObject(_fileName, dto) as Sprite;
		}

		public static function getDisplayObject(_fileName:String, _dto:ResObjectRequestParamDTO):DisplayObject
		{
			var resFile:ICEResFile = getResFileByFileName(_fileName);
			if (resFile != null)
			{
				return resFile.getObject(_dto) as DisplayObject;
			}
			CELog.log("Can't find file name : " + _fileName);
			return null;
		}

		public static function getBitmap(_symbolName:String, _fileName:String):BitmapData
		{
			var resFile:ICEResFile = getResFileByFileName(_fileName);
			if (resFile != null)
			{
				return resFile.getObject(ResDTOFactory.getBitmapDTO(_symbolName)) as BitmapData;
			}
			return null;
		}

		/**
		 *<b>根据Img的名称load Img</b>
		 */
		public static function getPicByFileName(_fileName:String):DisplayObject
		{
			return getDisplayObject(_fileName, null);
		}

		public static function getXML(_name:String):XML
		{
			return getResFileByFileName(_name).getObject(null) as XML;
		}

		public static function disposeFile(_fileName:String):void
		{
			getResFileByFileName(_fileName).dispose();
		}

		public static function isFileLoaded(_fileName:String):Boolean
		{
			var resFile:ICEResFile = getResFileByFileName(_fileName);
			return resFile.loadState == CEResConfig.LOAD_STATE_LOADED;
		}

		private static function getResFileByFileName(_fileName:String):ICEResFile
		{
			var resFile:ICEResFile;
			if (_fileName != null)
			{
				resFile = CEResManger.instance.getResFile(_fileName);
				if (resFile != null && resFile.loadState == CEResConfig.LOAD_STATE_UNLOAD)
				{
					CEResManger.instance.loadResFileByFileName(_fileName);
				}
				resFile == null && CELog.err("Can't find resFile Name : " + _fileName);
			}
			return resFile;
		}
	}
}
